package com.kis.controller.channel;

import com.kis.model.channel.Channel;
import com.kis.model.channel.Country;
import com.kis.model.channel.SendType;
import com.kis.service.channel.ChannelService;
import com.kis.service.commonService.CommonService;
import com.kis.tool.form.ChannelForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wanglong on 15-12-17.
 */
@Controller
public class ChannelController {
    @Autowired
    private ChannelService channelService;

    @RequestMapping("/")
    public String index(Model model) {
        List<Country> countryList = channelService.getCountrys();
        List<SendType> sendTypes = channelService.getSendType();
        model.addAttribute("country",countryList);
        model.addAttribute("sendType",sendTypes);
        return "/index";
    }
    @RequestMapping("/query")
    public String query(Model model,String sendType,Long countryId,Double weight) {
        List<Channel> channels = channelService.getBestChannel(countryId,weight,sendType);
        channels= channels.stream().filter(channel -> channel.getPrice()!=0).sorted((a,b)->a.getPrice().compareTo(b.getPrice())).collect(Collectors.toList());
        List<Country> countryList = channelService.getCountrys();
        List<SendType> sendTypes = channelService.getSendType();
        model.addAttribute("country",countryList);
        model.addAttribute("sendType",sendTypes);
        model.addAttribute("channels",channels);
        return "/index";
    }

//    @RequestMapping("/channel/add")
//    public String add(Model model) {
//        model.addAttribute("s", "2.0>2?0:2.0<0.07?0.07*80+9:2.0>0.2?2.0*75+9:2.0*80+9");
//        return "channel/addindex";
//    }
//
//    @RequestMapping("/channel/addSub")
//    public String addSub(ChannelForm channelForm) {
//        commonService.batchAddObject(channelForm.getChannelList());
//        System.out.println(channelForm.getChannelList().size());
//        return "redirect:/channel/add";
//    }
}
